<template>
    <view class="a-page">
        <view class="a-panel">

            <view class="a-cell">
                <input placeholder="目录名称" v-model="pathName"/>
            </view>
            <view class="a-cell">
                <textarea placeholder="json对象" v-model="jsonData" :maxlength="-1"></textarea>
            </view>
        </view>
        <view class="a-button-primary" @click="submit">
            开始下载
        </view>
        <my-confirm v-model="confirmVisible" :msg="confirmMsg" @confirm="confirmCallback"
                    :title="confirmTitle" :confirm-input="confirmInput"></my-confirm>
        <my-toptip ref="toptip"></my-toptip>
    </view>
</template>

<script>
    import {AppConfig} from "@/appConfig";
    import {Ajax, layer, Check} from "@/utils/index";
    import Layer from '@/tpl/layer'

    function getExt(url) {
        url = url.split('.');
        return url[url.length - 1];
    }

    export default {
        name: "down",
        mixins: [Layer],
        data() {
            return {
                pathName: '',
                jsonData: ''
            }
        },
        onNavigationBarButtonTap() {

        },
        onLoad() {
        },
        methods: {
            submit() {
                if (this.pathName.length === 0) {
                    layer.error('请输入目录名称');
                    return;
                }
                if (this.jsonData.length === 0) {
                    layer.error('请输入Json对象');
                    return;
                }
                const jsonData = JSON.parse(this.jsonData);
                console.log('jsonData', jsonData);
                Object.keys(jsonData).forEach((key) => {
                    const item = jsonData[key];
                    if (item.img) {
                        Ajax.post('http://127.0.0.1:89/api.ashx', {
                            action: 'down_img',
                            url: item.img,
                            pathName: this.pathName,
                            fileName: key + '.' + getExt(item.img)
                        })
                    }
                })
            }
        },
    }
</script>

<style scoped lang="less">
    textarea {
        height: 60vh;
    }

    input, textarea {
        border: 1px solid #EEEEEE;
        border-radius: 5px;
        padding: 5px;
    }
</style>
